//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SimpleCashFlow.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/CashFlow.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (Double amount, DateTime date) : CCashFlow(CSimpleCashFlow::typeid)
{
    try
    {
#ifdef HANDLE
        _phSimpleCashFlow = NULL;
#endif
        QuantLib::Real _amount = (QuantLib::Real)ValueHelper::Convert (amount); //d
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //d
        _ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (new QuantLib::SimpleCashFlow ( _amount,  _date ));
        SetCashFlow (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppSimpleCashFlow));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (boost::shared_ptr<QuantLib::SimpleCashFlow>& childNative, Object^ owner) : CCashFlow(CSimpleCashFlow::typeid)
{
#ifdef HANDLE
	_phSimpleCashFlow = NULL;
#endif
	_ppSimpleCashFlow = &childNative;
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppSimpleCashFlow));
}
Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (QuantLib::SimpleCashFlow& childNative, Object^ owner) : CCashFlow(CSimpleCashFlow::typeid)
{
#ifdef HANDLE
	_phSimpleCashFlow = NULL;
#endif
	_ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (&childNative);
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppSimpleCashFlow));
    _SimpleCashFlowOwner = owner;
    _CashFlowOwner = owner;
}

Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (CSimpleCashFlow^ copy) : CCashFlow(CSimpleCashFlow::typeid)
{
#ifdef HANDLE
	_phSimpleCashFlow = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (copy->GetShared());
        _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppSimpleCashFlow));
    }
}
Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (PLATFORM::Type^ t) : CCashFlow(CSimpleCashFlow::typeid)
{
#ifdef HANDLE
	_phSimpleCashFlow = NULL;
#endif
	if (!t->IsSubclassOf(CSimpleCashFlow::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (QuantLib::Handle<QuantLib::SimpleCashFlow>& childNative, Object^ owner)  : CCashFlow(CSimpleCashFlow::typeid)
{
	_phSimpleCashFlow = &childNative;
	_ppSimpleCashFlow = &static_cast<boost::shared_ptr<QuantLib::SimpleCashFlow>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppSimpleCashFlow));
    _SimpleCashFlowOwner = owner;
}
Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (QuantLib::Handle<QuantLib::SimpleCashFlow> childNative)  : CCashFlow(CSimpleCashFlow::typeid)
{
	_phSimpleCashFlow = &childNative;
	_ppSimpleCashFlow = &static_cast<boost::shared_ptr<QuantLib::SimpleCashFlow>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppSimpleCashFlow));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CSimpleCashFlow::CSimpleCashFlow (QuantLib::SimpleCashFlow childNative)  : CCashFlow(CSimpleCashFlow::typeid)
{
#ifdef HANDLE
	_phSimpleCashFlow = NULL;
#endif
	_ppSimpleCashFlow = new boost::shared_ptr<QuantLib::SimpleCashFlow> (new QuantLib::SimpleCashFlow (childNative));
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppSimpleCashFlow));
}
#endif

Cephei::QL::Cashflows::CSimpleCashFlow::~CSimpleCashFlow ()
{
    if (_ppSimpleCashFlow != NULL)
    {
	    delete _ppSimpleCashFlow;
        _ppSimpleCashFlow = NULL;
    }
}
Cephei::QL::Cashflows::CSimpleCashFlow::!CSimpleCashFlow ()
{
    if (_ppSimpleCashFlow != NULL)
    {
	    delete _ppSimpleCashFlow;
    }
}
QuantLib::SimpleCashFlow& Cephei::QL::Cashflows::CSimpleCashFlow::GetReference ()
{
    if (_ppSimpleCashFlow == NULL) throw REFNEW NativeNullException ();
	return **_ppSimpleCashFlow;
}
boost::shared_ptr<QuantLib::SimpleCashFlow>& Cephei::QL::Cashflows::CSimpleCashFlow::GetShared ()
{
    if (_ppSimpleCashFlow == NULL) throw REFNEW NativeNullException ();
	return *_ppSimpleCashFlow;
}
QuantLib::SimpleCashFlow* Cephei::QL::Cashflows::CSimpleCashFlow::GetPointer ()
{
    if (_ppSimpleCashFlow == NULL) throw REFNEW NativeNullException ();
	return &**_ppSimpleCashFlow;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SimpleCashFlow>& Cephei::QL::Cashflows::CSimpleCashFlow::GetHandle ()
{
	if (_phSimpleCashFlow == NULL)
	{
		_phSimpleCashFlow = new Handle<QuantLib::SimpleCashFlow> (*_ppSimpleCashFlow);
	}
	return *_phSimpleCashFlow;
}
#endif
bool Cephei::QL::Cashflows::CSimpleCashFlow::HasNative () 
{
	return (_ppSimpleCashFlow != NULL);
}

Double Cephei::QL::Cashflows::CSimpleCashFlow::Amount::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSimpleCashFlow)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CSimpleCashFlow::Date::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSimpleCashFlow)->date ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::ISimpleCashFlow^ Cephei::QL::Cashflows::CSimpleCashFlow_Factory::Create (Double amount, DateTime date)
{
    return REFNEW CSimpleCashFlow ( amount,  date);
}
